/*
	Written By Pradipna Nepal
	www.pradsprojects.com

	Copyright (C) 2011 Prads Projects
	Please read COPYING.txt included along with this source code for more detail.
	If not included, see http://www.gnu.org/licenses/
*/

#include "libGUIFunctions.h"

void GUIFunctions::createControls(HWND hwnd, HBITMAP *tbBitmaps, HFONT *controlsFont) {
	INITCOMMONCONTROLSEX commctrl;
	commctrl.dwSize = sizeof(INITCOMMONCONTROLSEX);
	commctrl.dwICC = ICC_BAR_CLASSES;
	InitCommonControlsEx(&commctrl);

	HWND tBar = CreateWindow(TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS,
		0, 0, 0, 0, hwnd, (HMENU)IDC_MAIN_TOOLBAR, GetModuleHandle(NULL), NULL);
	if (tBar != NULL) {
		SendMessage(tBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
		SendMessage(tBar, TB_SETBITMAPSIZE, 0, MAKELPARAM(24, 24));
		SendMessage(tBar, TB_SETMAXTEXTROWS, 0, 0);

		TBBUTTON tbButton[NUM_OF_TB_BTNS];
		TBADDBITMAP tbAddBitmap;

		tbAddBitmap.hInst = NULL;
		ZeroMemory(tbButton, sizeof(tbButton));
		tbButton[0].iString = (INT_PTR)"New Project";
		tbButton[1].iString = (INT_PTR)"Add File";
		tbButton[2].iString = (INT_PTR)"Open";
		tbButton[3].iString = (INT_PTR)"Save";
		tbButton[4].iString = (INT_PTR)"Delete File";
		tbButton[5].iString = (INT_PTR)"Compile";
		tbButton[6].iString = (INT_PTR)"Run";
		
		DWORD btnFace = GetSysColor(COLOR_BTNFACE);

		COLORMAP colorMap;
		colorMap.from = RGB(255, 0, 255);
		colorMap.to = btnFace;

		for (int i = 0; i < NUM_OF_TB_BTNS; ++i) {
			//Bitmaps defined in resource.h
			tbBitmaps[i] = CreateMappedBitmap(GetModuleHandle(NULL), 1000 + i, 0, &colorMap, 1);
			tbAddBitmap.nID = (UINT_PTR)tbBitmaps[i];
			tbButton[i].iBitmap = SendMessage(tBar, TB_ADDBITMAP, 1, (LPARAM)&tbAddBitmap);
			tbButton[i].fsState = TBSTATE_ENABLED;
			tbButton[i].fsStyle = TBSTYLE_BUTTON;
			tbButton[i].idCommand = 101 + i;	//ToolBar Buttons defined in resource.h
		}

		if (SendMessage(tBar, TB_ADDBUTTONS, NUM_OF_TB_BTNS, (LPARAM)tbButton)) {
			SendMessage(tBar, TB_SETBUTTONSIZE, 0, MAKELPARAM(36, 36));
			SendMessage(tBar, TB_AUTOSIZE, 0, 0);
			ShowWindow(tBar, SW_SHOW);	
		} else {
			MessageBox(hwnd, "Cannot Add Buttons in ToolBar", "Error", MB_ICONERROR);
		}
	} else { 
		MessageBox(hwnd, "Error Creating ToolBar", "Error", MB_ICONERROR);
	}

	CreateWindow("COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
		5, 43, 670, 150, hwnd, (HMENU)IDC_MAIN_CB_SOURCEFILES, GetModuleHandle(NULL), NULL);

	CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | 
		ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_MULTILINE | WS_HSCROLL,
		5, 75, 670, 389, hwnd, (HMENU)IDC_MAIN_TXT_SOURCECODE, GetModuleHandle(NULL), NULL);

	*controlsFont = CreateFont(0, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Courier New");

	SendDlgItemMessage(hwnd, IDC_MAIN_CB_SOURCEFILES, WM_SETFONT, (WPARAM)(*controlsFont), TRUE);
	SendDlgItemMessage(hwnd, IDC_MAIN_TXT_SOURCECODE, WM_SETFONT, (WPARAM)(*controlsFont), TRUE);

	HWND sBar = CreateWindow(STATUSCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 
		0, 0, 0, 0, hwnd, (HMENU)IDC_MAIN_STATUSBAR, GetModuleHandle(NULL), NULL);

	if (sBar == NULL) {
		MessageBox(hwnd, "Error Creating Status Bar", "Error", MB_ICONERROR);
		return;
	}

	int sBarWidth[] = {400, -1};
	SendMessage(sBar, SB_SETPARTS, sizeof(sBarWidth)/sizeof(int), (LPARAM)sBarWidth);
}

void GUIFunctions::resizeControls(HWND hwnd, int wndWidth, int wndHeight) {
	SetWindowPos(GetDlgItem(hwnd, IDC_MAIN_CB_SOURCEFILES), HWND_TOP, 
		0, 0, wndWidth - 10, 150, SWP_NOMOVE);
	
	SetWindowPos(GetDlgItem(hwnd, IDC_MAIN_TXT_SOURCECODE), HWND_TOP, 
		0, 0, wndWidth - 10, wndHeight - 101, SWP_NOMOVE);

	SendDlgItemMessage(hwnd, IDC_MAIN_TOOLBAR, TB_AUTOSIZE, 0, 0);
	SendDlgItemMessage(hwnd, IDC_MAIN_STATUSBAR, WM_SIZE, 0, 0);
}